<?php
sql();
if ($_POST['add']!=''){
	$artist='';
	$cmd="SELECT * FROM `select`";
	$excute=mysql_query($cmd);
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
		if (isset($_POST['artist'.$info['select_id']])) $artist.=$info['select_id'].'-';
	
	$songname=$_POST['songname'];
	$songstatus=$_POST['songstatus'];
	$info=$_POST['info'];
	$time=time();
	$songtype=$_POST['songtype'];
	
	$linkimg=''; 
		if ((($_FILES["file"]["type"] == "image/gif")
			|| ($_FILES["file"]["type"] == "image/jpeg")
			|| ($_FILES["file"]["type"] == "image/jpg")
			|| ($_FILES["file"]["type"] == "image/png")))
  		{
  			if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  		else{
         		chdir('/home/cailuong/public_html/beta/data/image/album/');
		 		$newname=rand(1,100000).time().$_FILES["file"]["name"];
         		move_uploaded_file($_FILES["file"]["tmp_name"],$newname);
		 		$linkimg='data/image/album/'.$newname;
				echo 'Upload ảnh thành công ! ';
  			}
	   }

	$tag=tag($songname).','.tag(convert($songname));
	$cmd="INSERT INTO `album` (album_artist,album_name,album_cat,album_img,album_detail,album_tag,album_time,album_info) 
	VALUES ('$artist','$songname','$songtype','$linkimg','$info','$tag','$time','$songstatus')";
	mysql_query($cmd);
	
    $cmd="SELECT * FROM album ORDER BY album_id DESC LIMIT 0,1";
	$info=mysql_fetch_assoc(mysql_query($cmd));
	$hashcode=hashencode($info['album_id']);
	$md5=md5($info['album_id'].'memhuynh');
	$cmd="UPDATE `album` SET album_md5='".$md5."',album_hash='".$hashcode."' WHERE album_id=".$info['album_id'];
	mysql_query($cmd);
	
	$cmd="SELECT * FROM  `album` ORDER BY  `album_id` DESC LIMIT 0 , 1";
	$rows=mysql_fetch_assoc(mysql_query($cmd));
	$thisvalue=$rows['album_id'];
	$cmd="SELECT * FROM `media`";
	$excute=mysql_query($cmd);
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC)){
		if (isset($_POST['song'.$info['media_id']])) 
		{
			$cmd="UPDATE `media` SET media_album=".$thisvalue." WHERE media_id=".$info['media_id'];
			mysql_query($cmd);
		} else				
		if ($info['media_album']==$_GET['edit'])
		{
			$cmd="UPDATE `media` SET media_album=0 WHERE media_id=".$info['media_id'];
			mysql_query($cmd);
		}
	}
	
	$_POST['submit']='';
	echo 'Đã thêm album này thành công !';
	echo '<div id="pre"><div class="buttons"><a href="/mem/?menu=select&act=add" class="button">Thêm nữa</a></div></div><div id="next"><div class="buttons"><a href="/mem/?menu=select" class="button">Quay lại</a></div></div>';
} else
if ($_POST['edit']!='')
{
sql();
	$artist='';
	$cmd="SELECT * FROM `select`";
	$excute=mysql_query($cmd);
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
		if (isset($_POST['artist'.$info['select_id']])) $artist.=$info['select_id'].'-';
	
	$cmd="SELECT * FROM `media`"; 
	$excute=mysql_query($cmd);
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
	{
		if (isset($_POST['song'.$info['media_id']])) 
		{
		    $mediaorder=$_POST['order'.$info['media_id']];
			$cmd="UPDATE `media` SET media_album=".$_GET['edit'].",media_order=".$mediaorder." WHERE media_id=".$info['media_id'];
			mysql_query($cmd);
		} else				
		if ($info['media_album']==$_GET['edit'])
		{
			$cmd="UPDATE `media` SET media_album=0 WHERE media_id=".$info['media_id'];
			mysql_query($cmd);
		}
	}
	$songname=$_POST['songname'];
	$songstatus=$_POST['songstatus'];
	$inf=$_POST['homepage'];
	$info=$_POST['info'];
	$time=time();
	$songtype=$_POST['songtype'];
	$linkimg='';
		if ((($_FILES["file"]["type"] == "image/gif")
			|| ($_FILES["file"]["type"] == "image/jpeg")
			|| ($_FILES["file"]["type"] == "image/jpg")
			|| ($_FILES["file"]["type"] == "image/png")))
  		{
  			if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  		else{
         		chdir('/home/cailuong/public_html/beta/data/image/album/');
		 		$newname=rand(1,100000).time().$_FILES["file"]["name"];
         		move_uploaded_file($_FILES["file"]["tmp_name"],$newname);
		 		$linkimg='data/image/album/'.$newname;
				echo 'Upload ảnh thành công ! ';
  			}
	   }
	if ($linkimg=='') $wlinkimg=''; else $wlinkimg='album_img="'.$linkimg.'",';
	$tag=tag($songname).','.tag(convert($songname));
	$cmd="UPDATE `album` SET album_artist='$artist',album_set='$inf',album_name='$songname',album_cat='$songtype',".$wlinkimg."album_detail='$info',album_tag='$tag',album_time='$time',album_info='$songstatus' ". "WHERE album_id=".$_GET['edit'];
	mysql_query($cmd);
	$_POST['submit']='';
	echo 'Đã chỉnh sửa thông tin album này thành công !';
	echo '<div id="pre"><div class="buttons"><a href="/mem/?menu=select&act=add" class="button">Thêm nữa</a></div></div><div id="next"><div class="buttons"><a href="/mem/?menu=select" class="button">Quay lại</a></div></div>';
} else
{
if (isset($_GET['act'])){
	$imagelink=$info['album_img'];
	$audiocheck=false;
	$videocheck=false;
	if (substr($link,strlen($link)-3,strlen($link))=='mp3'){$audiocheck=true; $linv='';}else
	{$linkv=$link; $videocheck=true;}
echo '<div class="selecttitle"><a><b>THÊM ALBUM</b></a></div><br/>
	<form method="post" enctype="multipart/form-data">
	<table border="0">
	<tr>
    <td width="90px"><div class="selecttitle"><a>Tình trạng</a></div></td>
    <td><div class="selecttitle"><a><select name="songstatus">';
		echo '<option value="0">Chưa có dữ liệu</option>
		<option value="1">Còn thiếu dữ liệu</option>
		<option value="2">Dữ liệu đã đầy đủ</option>';
	echo'</select></a></div></td>
    </tr>
    <tr>
    <td width="90px"><div class="selecttitle"><a>Tên album</a></div></td>
    <td><div class="selecttitle"><a><input name="songname" type="text"/></a></div></td>
    </tr>
    <tr>
    <td><div class="selecttitle"><a>Thể loại</a></div></td>
    <td><div class="selecttitle"><a><select name="songtype">';
	$cmd="SELECT * FROM `cat`";
	$excute=mysql_query($cmd);
	while ($rows=mysql_fetch_array($excute,MYSQL_ASSOC))
	{
		$value=$rows['cat_id'];
		if ($rows['cat_id']==$info['album_cat']) $values="selected"; else $values='';
		echo '<option value="'.$value.'" '.$values.'>'.$rows['cat_name'].'</option>';
	}
	echo'</select></a></div></td>
    </tr>
    </table><br/>';
echo'<div class="selecttitle"><a>Tải ảnh album lên &nbsp;&nbsp;<input type="file" name="file" id="file"/><a>
	</div>

	<div class="selecttitle"><a><b>Thông tin Album</b> <br/><br/><textarea name="info"></textarea><a></div>
	<input type="submit" class="buttonclick" value="Lưu" name="add"/>';
	echo'<div id="songbox" style="width:280px; height: 400px;">
	<b>HỘP CHỌN BÀI HÁT</b>';
	echo '<div class="listbox3">';	
	$cmd="SELECT * FROM `media` ORDER BY media_id DESC LIMIT 0,100";
	$excute=mysql_query($cmd);
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
		if ($info['media_album']!=$edit)
echo '<div class="listitem3" selectid="'.$info['media_id'].'"><input type="checkbox" name="'.'song'.$info['media_id'].'" id="box3'.$info['media_id'].'" /><a>'.readmore($info['media_name'],5).'</a></div>';
	echo'</div></div>

	<div id="artistbox2" style="width:200px; height:440px;">
	<b>HỘP CHỌN NGHỆ SĨ</b>';
	$cmd="SELECT * FROM `select` WHERE (select_type=3) OR (select_type=1) ORDER BY `select_art` ASC";
	$excute=mysql_query($cmd);
	echo '<div class="listbox">';
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
echo '<div class="listitem1" artistid="'.$info['select_id'].'"><input type="checkbox" name="'.'artist'.$info['select_id'].'"  id="box1'.$info['select_id'].'" /><a>'.$info['select_art'].'</a></div>';
	echo'</div>
	</div>
	</form>';
}
else
if (isset($_GET['edit']))
{
$edit=$_GET['edit'];
	$sql="SELECT * FROM `album` WHERE album_id=".$edit;
	$info=mysql_fetch_assoc(mysql_query($sql));
	$link=$info['album_link'];
	$authorarray=encode($info['album_author']);
	$artistarray=encode($info['album_artist']);
	$imagelink=$info['album_img'];
	$audiocheck=false;
	$videocheck=false;
	if (substr($link,strlen($link)-3,strlen($link))=='mp3'){$audiocheck=true; $linv='';}else
	{$linkv=$link; $videocheck=true;}
	$cmd="SELECT * FROM `album` WHERE album_id=".$_GET['edit'];
	$row=mysql_fetch_assoc(mysql_query($cmd));
echo '<div class="selecttitle"><a><b>SỬA ALBUM '.readmore($info['album_name'],5).'</b></a></div><br/>
	<form method="post" enctype="multipart/form-data">
	<table border="0">
	<tr>
    <td width="90px"><div class="selecttitle"><a>Trang chủ</a></div></td>
    <td><div class="selecttitle"><a><select name="homepage">
		<option '.(($row['album_set']==1)?'selected':'').' value="1">Có</option>
		<option '.(($row['album_set']==0)?'selected':'').' value="0">Không</option> 
	</select></a></div></td>
    </tr>
	<tr>
    <td width="90px"><div class="selecttitle"><a>Tình trạng</a></div></td>
    <td><div class="selecttitle"><a><select name="songstatus">';
	if ($row['album_info']==0) $s0='selected';
	if ($row['album_info']==1) $s1='selected';
	if ($row['album_info']==2) $s2='selected';

		echo '<option value="0" '.$s0.'>Chưa có dữ liệu</option>
		<option value="1" '.$s1.'>Còn thiếu dữ liệu</option>
		<option value="2" '.$s2.'>Dữ liệu đã đầy đủ</option>';
	echo'</select></a></div></td>
    </tr>
    <tr>
    <td width="90px"><div class="selecttitle"><a>Tên album</a></div></td>
    <td><div class="selecttitle"><a><input name="songname" value="'.$info['album_name'].'" type="text"/></a></div></td>
    </tr>
    <tr>
    <td><div class="selecttitle"><a>Thể loại</a></div></td>
    <td><div class="selecttitle"><a><select name="songtype">';
	$cmd="SELECT * FROM `cat`";
	$excute=mysql_query($cmd);
	while ($rows=mysql_fetch_array($excute,MYSQL_ASSOC))
	{
		$value=$rows['cat_id'];
		if ($rows['cat_id']==$info['album_cat']) $values="selected"; else $values='';
		echo '<option value="'.$value.'" '.$values.'>'.$rows['cat_name'].'</option>';
	}
	echo'</select></a></div></td>
   
    </table>';

	echo  '<img src="../'.$imagelink.'" width="100px"/><br/>
	<br/>';
echo'<div class="selecttitle"><a>Tải ảnh album lên &nbsp;&nbsp;<input type="file" name="file" id="file"/><a>
	</div>

	<div class="selecttitle"><a><b>Thông tin Album</b> <br/><br/><textarea name="info">'.$row['album_detail'].'</textarea><a></div>
	<input type="submit" class="buttonclick" value="Sửa" name="edit"/>
	<input type="button" class="buttonclick" value="Sắp xếp" id="sapxepbox"/>
	';
	echo'
	<div id="songbox" style="width:280px; height: 400px;">
	<b>HỘP CHỌN BÀI HÁT</b>';
	echo '<div class="listbox3">';
	$cmd="SELECT * FROM `media` WHERE media_album=".$edit." ORDER BY `media_order` ASC";
	$excute=mysql_query($cmd);
	$dem=0;
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
	{
echo '<div class="listitem3" selectid="'.$info['media_id'].'"><input type="checkbox" checked name="'.'song'.$info['media_id'].'"  id="box3'.$info['media_id'].'" /><a>'.readmore($info['media_name'],5).'</a></div>';
echo '<div class="order"><select name="order'.$info['media_id'].'">';
for ($i=1;$i<=20;$i++)
{
    echo '<option value="'.$i.'" ';
    if ($i==$info['media_order']) echo 'selected';
	echo'>'.$i.'</option>';
}
echo'</select></div>';
    }
	$cmd="SELECT * FROM `media` ORDER BY media_id DESC LIMIT 0,100";
	$excute=mysql_query($cmd);
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
		if ($info['media_album']!=$edit)
echo '<div class="listitem3" selectid="'.$info['media_id'].'"><input type="checkbox" name="'.'song'.$info['media_id'].'" id="box3'.$info['media_id'].'" /><a>'.readmore($info['media_name'],5).'</a></div>';
	
	echo'</div></div>

	<div id="artistbox2" style="width:200px; height:440px;">
	<b>HỘP CHỌN NGHỆ SĨ</b>';
	$cmd="SELECT * FROM `select` WHERE (select_type=3) OR (select_type=1) ORDER BY `select_art` ASC";
	$excute=mysql_query($cmd);
	echo '<div class="listbox">';
	while($info=mysql_fetch_array($excute,MYSQL_ASSOC))
	{
		if (in_array($info['select_id'],$artistarray)) $valuep=' checked '; else $valuep='';
echo '<div class="listitem1" artistid="'.$info['select_id'].'"><input type="checkbox"'.$valuep.' name="'.'artist'.$info['select_id'].'"  id="box1'.$info['select_id'].'" /><a>'.$info['select_art'].'</a></div>';
	}

	echo'</div>
	</div>
	</form>';

}
else
{
if (isset($_GET['del']))
{
	$sql="DELETE FROM `album` WHERE album_id=".$_GET['del'];
	mysql_query($sql);
}
if (isset($_GET['x']))
{
	$x=$_GET['x'];
	$decy=$x-15;
	if ($decy<0) $decy=0;
	$incy=$x+15;
} else {$x=0; $decy=0; $incy=15;}
echo '<div id="next"><div class="buttons"><a href="/mem/?menu=album&x='.$incy.'" class="button">Tiến tới</a></div></div>
<div id="pre"><div class="buttons"><a href="/mem/?menu=album&x='.$decy.'" class="button">Lùi lại</a></div></div>';
?>
<div id="space"><div class="buttons"><a href="?menu=album&act=add" class="button add">Thêm</a></div></div>
<div id="search">
<form method="post" action="/mem/?menu=album&search=keyword">
<input type="text" name="tukhoa" autocomplete="off" class="search" value="Nhập nội dung cần tìm"  onblur="if(this.value=='') this.value='Nhập nội dung cần tìm';" onFocus="if(this.value=='Nhập nội dung cần tìm') this.value='';" />
</form></div>
<div id="allpress"><div class="buttons"><a href="/mem/?menu=album&set=press" class="button">Thiết lập</a></div></div>
<?php
echo '
<div id="view">
<table width="775" border="1" class="view" cellpadding="10" cellspacing="0">
  <th width="30px">ID</th>
  <th>Tên album</th>
  <th width="70px">Số lượng bài</th>
  <th width="80px">Người đăng</th>
  <th width="110px">Thời gian update</th>
  <th width="65px">Tình trạng</th>
  <th width="10px">Sửa</th>
  <th width="10px">Xóa</th>';
	if (isset($_GET['search']))
	$sql="SELECT * FROM `album` WHERE album_tag LIKE '%".$_POST['tukhoa']."%'";
	else
	$sql='SELECT * FROM `album` ORDER BY  `album_id` DESC LIMIT '.$x.',15';
	  $excute=mysql_query($sql);
	
	  while ($row=mysql_fetch_array($excute,MYSQL_ASSOC))
	  {
		  echo '<tr><td>'.$row['album_id']; echo '</td>';
		  echo '<td>'.$row['album_name']; echo '</td>';
		  $cmd="SELECT * FROM `media` WHERE media_album=".$row['album_id'];
		  echo '<td>'.mysql_num_rows(mysql_query($cmd)).'</td>';
		  echo '<td></td>';
		  echo '<td>'.timepro($row['album_time']).'</td>';
		  if  ($row['album_info']==0) echo '<td class="redcolor"></td>'; else
		  if  ($row['album_info']==1) echo '<td class="yellowcolor"></td>'; else
		  if  ($row['album_info']==2) echo '<td class="bluecolor"></td>';

echo '<td><div class="buttons"><a class="button flag" href="?menu=album&edit='.$row['album_id'].'">Sửa</a></div></td>';
echo '<td><div class="buttons"><a class="button delete" href="?menu=album&del='.$row['album_id'].'">Xóa</a></div></td></tr>';
	  }
echo '</table></div>';
}
}?>


